import { mapTree } from 'amis/lib/utils/helper'

export const schema = {
  type: 'page',
  body: {
    type: 'lib-crud',
    api: '$preset.apis.list',
    filter: '$preset.forms.filter',
    filterTogglable: true,
    perPageField: 'size',
    pageField: 'page',
    perPageAvailable: [50, 100, 200],
    defaultParams: {
      size: 50,
    },
    headerToolbar: [
      'filter-toggler',
      {
        type: 'columns-toggler',
        align: 'left',
      },

      {
        $preset: 'actions.add',
        align: 'right',
      },
    ],
    footerToolbar: ['statistics', 'switch-per-page', 'pagination'],
    columns: [

      {
        name: 'field_desc',
        label: '字段描述',
        type: 'text',
      },
      {
        name: 'field_name',
        label: '字段标识',
        type: 'text',
      },
      {
        name: 'field_type',
        label: '字段类型',
        type: 'text',
      },
      {
        name: 'field_length',
        label: '字段长度',
        type: 'text',
      },
      {
        name: 'field_default',
        label: '字段默认值',
        type: 'text',
      },
      {
        name: 'field_null',
        label: 'null',
        type: 'text',
      },
      {
        name: 'field_index',
        label: '索引',
        type: 'text',
      },


      {
        name: 'desc',
        label: '备注',
        type: 'tpl',
        tpl: '<span class="text-ellipsis" title="${field_remark}">${field_remark}</span>',
        width: 150,
      },
      {
        name: 'create_time',
        label: '创建时间',
        type: 'datetime',
      },
      {
        name: 'update_time',
        label: '更新时间',
        type: 'datetime',
      },
      {
        name: 'operator_name',
        label: '操作人',
        type: 'text',
      },
      {
        type: 'operation',
        label: '操作',
        width: 60,
        limits: ['editItem'],
        limitsLogic: 'or',
        buttons: ['$preset.actions.edit','$preset.actions.copy', '$preset.actions.del'],
      },
    ],
  },
  definitions: {
    updateControls: {
      controls: [
        {
          type: 'hidden',
          // type: 'text',
          name: 'model',
        },

        {
          type: 'text',
          name: 'field_desc',
          label: '字段描述',
          required: true,
        },
        {
          type: 'text',
          name: 'field_name',
          label: '字段标识',
          required: true,
        },
        {
          name: 'field_type',
          label: '字段类型',
          required: true,
          type: 'select',
          options: [
            {
              label: 'varchar',
              value: 'varchar',
            },
            {
              label: 'int',
              value: 'int',
            },
            {
              label: 'text',
              value: 'text',
            },
            {
              label: 'datetime',
              value: 'datetime',
            },
          ],
        },
        {
          type: 'number',
          name: 'field_length',
          label: '字段长度',
          requiredOn: "this.field_type === 'varchar'",
        },
        {
          type: 'text',
          name: 'field_default',
          label: '字段默认值',
          required: false,
        },
        {
          name: 'field_null',
          label: 'null',
          required: true,
          type: 'checkbox',
          trueValue: 1,
          falseValue: 0,
        },
        {
          name: 'field_index',
          label: '索引',
          required: false,
          type: 'select',

          options: [
            {
              label: 'unique',
              value: 'unique',
            },
            {
              label: 'index',
              value: 'index',
            },
          ],
        },

        {
          type: 'text',
          name: 'field_remark',
          label: '字段描述',
          required: false,
        },
      ],
    },
  },
  preset: {
    actions: {
      add: {
        limits: 'editItem',
        type: 'button',
        align: 'right',
        actionType: 'dialog',
        label: '添加',
        icon: 'fa fa-plus pull-left',
        size: 'sm',
        primary: true,
        dialog: {
          title: '新增',
          body: {
            type: 'form',
            name: 'sample-edit-form',
            api: '$preset.apis.add',
            $ref: 'updateControls',
          },
        },
      },

      edit: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-pencil',
        tooltip: '编辑',
        actionType: 'dialog',
        dialog: {
          title: '编辑',
          body: {
            type: 'form',
            name: 'sample-edit-form',
            api: '$preset.apis.edit',
            $ref: 'updateControls',
            
          },
        },
      },
      copy: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-files-o',
        tooltip: '复制',
        actionType: 'dialog',
        dialog: {
          title: '复制',
          body: {
            type: 'form',
            name: 'sample-edit-form',
            api: '$preset.apis.add',
            $ref: 'updateControls',
            
          },
        },
      },
      del: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-times text-danger',
        actionType: 'ajax',
        tooltip: '删除',
        confirmText: '您确认要删除?',
        api: '$preset.apis.del',
        messages: {
          success: '删除成功',
          failed: '删除失败',
        },
      },
    },
    forms: {
      filter: {
        controls: [
          {
            type: 'text',
            name: 'search',
            label: '关键字',
            clearable: true,
            placeholder: '',
          },

          {
            type: 'submit',
            className: 'm-l',
            label: '搜索',
            level: 'primary',
          },
          {
            type: 'wrapper',
            className: 'no-wrapper m-l a-Form-item a-Form-item--inline ',
            body: [
              {
                type: 'action',
                actionType: 'link',
                link: '/demo/model/user_app_fields',
                label: '重置',
              },
            ],
          },
        ],
      },
    },
  },
}

const options = {
  props: {
    locale: 'en',
  },
}

export default options
